package test.liyi;

public class MyCircle {
//代表圆，圆心及半径是他的属性，计算圆的面积，周长，判断两个圆是否相交，判断给定的直线与给定圆的位置关系
	private double radius;
	private int center_x;
	private int center_y;
	private double PI = 3.1415926;
	public double getRadius() {
		return this.radius;
	}
	public int getCenter_X() {
		return this.center_x;
	}
	public int getCenter_Y() {
		return this.center_y;
	}
	public MyCircle(double radius, int center_x, int center_y) {
		this.radius = radius;
		this.center_x = center_x;
		this.center_y = center_y;
	}
	//得到园的面积
	public double getArea() {
		return PI * radius * radius;
	}
	//得到圆的周长
	public double getPermeter() {
		return PI * radius * 2;
	}
	
	private Double getLenWithPoints(double p1x, double p1y, double p2x, double p2y) {
		Double length = null;
		length = Math.sqrt(Math.pow(p2x - p1x, 2) + Math.pow(p2y - p1y, 2));
		return length;
	}

	/**
	 * 1 相离
	 * 2 相切
	 * 3 相交
	 * 0 错认
	 * @param lx1
	 * @param ly1
	 * @param lx2
	 * @param ly2
	 * @return
	 */
	private Double getLength(double lx1, double ly1, double lx2, double ly2) {
		Double length = null;
		double b = getLenWithPoints(lx1, ly1, this.center_x, this.center_y);
		double c = getLenWithPoints(lx2, ly2, this.center_x, this.center_y);
		double a = getLenWithPoints(lx1, ly1, lx2, ly2);
 
		if (c + b == a) {// 点在线段上
			length = (double) 0;
		} else if (c * c >= a * a + b * b) { // 组成直角三角形或钝角三角形，投影在point1延长线上，
			length = b;
		} else if (b * b >= a * a + c * c) {// 组成直角三角形或钝角三角形，投影在point2延长线上，
			length = c;
		} else {
			// 组成锐角三角形，则求三角形的高
			double p = (a + b + c) / 2;// 半周长
			double s = Math.sqrt(p * (p - a) * (p - b) * (p - c));// 海伦公式求面积
			length = 2 * s / c;// 返回点到线的距离（利用三角形面积公式求高）
		}
 
		return length;
	}
	public int getLineAboutCircle(double lx1, double ly1, double lx2, double ly2) {
		double len = getLength(lx1,ly1,lx2,ly2);
		if(len > this.radius) {
			return 1;
		}else if (len == this.radius) {
			return 2;
		}else if(len < this.radius){
			return 3;
		}else {
			return 0;
		}
	}
	//判斷兩個園是否相交
	public boolean isIntersect(MyCircle circle) {
		return 3==getRelationship(circle) ? true : false;
	}
	/**
	 * 1 相离
	 * 2 相切
	 * 3 相交
	 * 4 内切
	 * 5 内含
	 * 0 错误
	 * @param circle
	 * @return
	 */
	public int getRelationship(MyCircle circle) {
		double x = Math.pow(circle.getCenter_X() + this.center_x, 2);
		double y = Math.pow(circle.getCenter_Y() + this.center_y, 2);
		double distance = Math.sqrt(x+y);
		double dr = circle.getRadius() + this.radius;
		double or = circle.getArea() > this.radius ? circle.getArea()-this.radius : this.radius-circle.getArea();
		if(distance > dr) {
			return 1;
		}else if(distance == dr) {
			return 2;
		}else if (or < distance && distance < dr) {
			return 3;
		}else if (distance == or) {
			return 4;
		}else if(0 <= distance && distance < or) {
			return 5;
		}else {
			return 0;
		}
		
	}
}
